//
//  WFWelcomeVC.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/13.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit

import SDWebImage
import SnapKit

class WFWelcomeVC: UIViewController {

    // MARK:  - 懒加载控件
    //背景图
    private lazy var bgImgView:UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "ad_background"))
        return imgView
    }()
    
    //头像
    private lazy var headIconImgView :UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        if let headImgUrl = WFUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large{
            imgView.sd_setImageWithURL(NSURL(string: headImgUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        imgView.layer.cornerRadius = imgView.bounds.size.width * 0.5
        imgView.layer.masksToBounds = true
        return imgView
    
    }()
    
    //消息
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.grayColor()
        
        //初始化的时候就设置名字
        if let name = WFUserAccountViewModel.sharedUserAccount.userAccount?.name{
            label.text = "欢迎回来,\(name)"
        }else{
            label.text = "欢迎回来"
        }
        
        label.alpha = 0
//        label.hidden = true
        
        return label
    }()
    
    //替换背景
    override func loadView() {
        view = bgImgView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        setUPUI()
    }
    
    // MARK:  - 设置动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }

    // MARK:  - 设置UI
    func setUPUI(){
        view.addSubview(headIconImgView)
        view.addSubview(messageLabel)
        
        headIconImgView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(150)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSizeMake(85, 85))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headIconImgView.snp_bottom).offset(20)
            make.centerX.equalTo(headIconImgView.snp_centerX)
        }
    }
    
    // MARK:  - 执行动画
    func startAnimation(){
        self.headIconImgView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        
        //执行阻尼动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //完毕之后,再执行label的动画
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
//                    self.messageLabel.hidden = false
                    }, completion: { (_) -> Void in
                        //进入首页
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                        
                })
        }
    }

}
